﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine;
using PloobsEngine.SceneControl;
using PloobsEngine.Utils;

namespace Etapa1.Screens
{
    public class NoiseScreen : IScreen
    {
        public NoiseScreen()
            : base()
        {
        }

        Effect effect;
        RenderTarget2D target;
        Texture2D staticRandomTex;
        Texture2D perlinNoise;
        RenderHelper render;

        bool firstTime = true;

        public override void Draw(GameTime gameTime)
        {
            if (firstTime)
            {                
                render.SetUpRenderTarget(target);
                float time = (float)gameTime.TotalGameTime.TotalMilliseconds / 100.0f;
                EngineStuff.GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
                effect.CurrentTechnique = effect.Techniques["PerlinNoise"];
                effect.Parameters["xTexture"].SetValue(staticRandomTex);
                effect.Parameters["xOvercast"].SetValue(1.1f);
                effect.Parameters["xTime"].SetValue(time / 1000.0f);                
                render.RenderTextureToFullScreenVertexPixel(effect);                
                perlinNoise = render.GetResultsRenderTarget();
                //perlinNoise.Save("perlin.png", ImageFileFormat.Png);
                //firstTime = false;
            }

            EngineStuff.SpriteBatch.Begin();
            EngineStuff.SpriteBatch.Draw(perlinNoise, Vector2.Zero, Color.White);
            EngineStuff.SpriteBatch.End();

            base.Draw(gameTime);

        }

        public override void LoadContent(EngineStuff es)
        {
            staticRandomTex = TextureCreator.CreateCompleteRandomColorTexture(32); ///tem q ser 32, pq o shader espera isso (nao vale a pena alterar o shader para deixar independente de resolucao)
            target = EngineStuff.GetDefaultColorBuffer();
            effect = EngineStuff.CustomContentManager.GetAsset<Effect>("Effects\\perlin");
            render = new RenderHelper(es);            
            base.LoadContent(es);
        }
    }
        
}

